// *******************************************************************************************************
// # エリア
//   ・検索結果 サイドナビゲーション
// *******************************************************************************************************

var ResultBridge = ResultBridge || {};

$(function() {
	var isUnderIe6 = uaInfo.isIe && uaInfo.ieVr <= 6,
		isUnderIe7 = uaInfo.isIe && uaInfo.ieVr <= 7;

	/*******************************************************
	 *　サイドナビ
	 *******************************************************/
	var $navi = $('#contentSubColumn');

	function toggoleCheckReload($title, $content){
		var b = false;
		if ($title.hasClass('active')) {
			$content.show();
			b = true;
		} else {
			$content.hide();
			b = false;
		};
		toggleChangeActive($title, $content, b);
	};

	function toggleChangeActive($title, $content, b){
		if (b) {
			$title.addClass('active');
			$title.find('a span').removeClass('icn_plus');
			$title.find('a span').addClass('icn_minus');
			$title.find('a span').text('閉じる');
		} else {
			$title.removeClass('active');
			$title.find('a span').removeClass('icn_minus');
			$title.find('a span').addClass('icn_plus');
			$title.find('a span').text('開く');
		};
	};

	/*******************************************************/
	// 地域
	$navi.find('.mod_box_sub.area .mod_box_area_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_ttl_child'),
				$content = $(this).find('.list_arealine');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendController.change();
			};
			toggoleCheckReload($title, $content);
		};
	});

	/*******************************************************/
	// 路線・駅
	$navi.find('.mod_box_sub.line .mod_box_area_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_ttl_child'),
				$content = $(this).find('.list_arealine');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendController.change();
			};
			toggoleCheckReload($title, $content);
		};
	});

	/*******************************************************/
	// 基本条件
	$navi.find('.mod_box_sub.basic .mod_box_sub_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_title'),
				$content = $(this).find('.box_content');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendController.change();
			};
			toggoleCheckReload($title, $content);
		};
	});

	// 賃料
	var rentSelecter = (function(){
		var that = {
			$target: $navi.find('.mod_box_sub.basic .mod_box_sub_child.rent ul.list_select'),
			$down: $navi.find('.mod_box_sub.basic .mod_box_sub_child.rent ul.list_select').find('li').eq(0),
			$up:   $navi.find('.mod_box_sub.basic .mod_box_sub_child.rent ul.list_select').find('li').eq(1),
			dwVal: 0,
			upVal: 9999999
		};
		function init(){
			that.dwVal = parseInt(that.$down.find('select').val());
			that.upVal = parseInt(that.$up.find('select').val());
			that.$down.on('change', function(e){
				that.dwVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
			that.$up.on('change', function(e){
				that.upVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
		};
		function check(){
			that.isChanging = true;
			if (that.dwVal != 0 && that.upVal != 0 && that.dwVal > that.upVal) {
				var _val1 = that.upVal;
				var _val2 = that.dwVal;
				that.dwVal = _val1;
				that.upVal = _val2;
				that.$down.find('select').val(_val1);
				that.$up.find('select').val(_val2);
				that.$down.find('.custom_selectInner').text(toSelectText(_val1));
				that.$up.find('.custom_selectInner').text(toSelectText(_val2));
			};
			that.isChanging = false;
		};
		function toSelectText(str){
			return str * 0.0001 + '万円';
		}
		init();
		return that;
	}());

	// 専有面積
	var exclusiveSelecter = (function(){
		var that = {
			$target: $navi.find('.mod_box_sub.basic .mod_box_sub_child.exclusive ul.list_select'),
			$down: $navi.find('.mod_box_sub.basic .mod_box_sub_child.exclusive ul.list_select').find('li').eq(0),
			$up:   $navi.find('.mod_box_sub.basic .mod_box_sub_child.exclusive ul.list_select').find('li').eq(1),
			dwVal: 0,
			upVal: 99999
		};
		function init(){
			that.dwVal = parseInt(that.$down.find('select').val());
			that.upVal = parseInt(that.$up.find('select').val());
			that.$down.on('change', function(e){
				that.dwVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
			that.$up.on('change', function(e){
				that.upVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
		};
		function check(){
			that.isChanging = true;
			if (that.dwVal != 0 && that.upVal != 0 && that.dwVal > that.upVal) {
				var _val1 = that.upVal;
				var _val2 = that.dwVal;
				that.dwVal = _val1;
				that.upVal = _val2;
				that.$down.find('select').val(_val1);
				that.$up.find('select').val(_val2);
				that.$down.find('.custom_selectInner').text(toSelectText(_val1));
				that.$up.find('.custom_selectInner').text(toSelectText(_val2));
			};
			that.isChanging = false;
		};
		function toSelectText(str){
			return str + 'm²';
		}
		init();
		return that;
	}());


	/*******************************************************/
	// 絞り込み条件
	$navi.find('.mod_box_sub.narrow .mod_box_sub_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_title'),
				$content = $(this).find('.box_content');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendController.change();
			};
			toggoleCheckReload($title, $content);
		};
	});

	/*******************************************************/
	// 親
	$navi.find('.mod_box_sub').each(function(i){
		$(this).find('.box_title').first().not('.non').each(function(j){
			var $title   = $(this),
				$content = $(this).next();
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
				});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendController.change();
			};
			toggoleCheckReload($title, $content);
		});
	});


	/*******************************************************
	 *　再検索
	 *******************************************************/
	var defInput  = [],
		defSelect = [];
	$('#contentSubColumn .mod_box_sub').find('input').each(function(i){
		if (!isUnderIe7) {
			$(this).on('ifChecked', function(e){
				checkUserContact();
			}).on('ifUnchecked', function(e){
				checkUserContact();
			});
		} else {
			$(this).on('click', function(e){
				checkUserContact();
			});
		};
		defInput.push($(this).prop('checked'));
	});
	$('#contentSubColumn .mod_box_sub').find('select').each(function(i){
		$(this).on('change', function(e){
			checkUserContact();
		});
		defSelect.push($(this).val());
	});

	function checkUserContact(){
		var isChange = false;
		$('#contentSubColumn .mod_box_sub').find('input').each(function(i){
			if (defInput[i] != $(this).prop('checked')) {
				isChange = true;
				return false;
			};
		});
		$('#contentSubColumn .mod_box_sub').find('select').each(function(i){
			if (defSelect[i] != $(this).val()) {
				isChange = true;
				return false;
			};
		});
		if (isChange) {
			appendController.show(2);
			$('#appendContainerWrapper .mod_btn_research a').removeClass('mod_btn_disabled');
			$('#contentSubColumn .btn_research a').removeClass('mod_btn_disabled');
		} else {
			appendController.hide(2);
			$('#appendContainerWrapper .mod_btn_research a').addClass('mod_btn_disabled');
			$('#contentSubColumn .btn_research a').addClass('mod_btn_disabled');
		};
	}


	/*******************************************************
	 *　まとめて問い合わせラッパー
	 *******************************************************/
	var appendWrapper = new APMN.AppendWrapper({
		isFixed: false,
		ieVr: !(isUnderIe6)
	});

	// bridge
	ResultBridge.appendShow = function(){
		appendController.show(1);
	};
	ResultBridge.appendHide = function(){
		appendController.hide(1);
	};
	ResultBridge.appendChange = function(){
		appendController.change();
	};

	var appendController = (function(){
		var that ={
			isShow: false,
			isInquiry: false,
			isContact: false
		};
		that.show = function(n){
			if (n == 1) that.isInquiry = true;
			else that.isContact = true;
			if (!that.isShow) {
				that.isShow = true;
				appendWrapper.show();
			};
		};
		that.hide = function(n){
			if (n == 1) that.isInquiry = false;
			else that.isContact = false;
			if (that.isShow && !that.isContact && !that.isInquiry) {
				that.isShow = false;
				appendWrapper.hide();
			};
		};
		that.change = function(){
			appendWrapper.changeCtsHeight();
			if (ResultBridge.callBackCtsHeightEvent) {
				ResultBridge.callBackCtsHeightEvent();
			};
		};
		return that;
	}());
});